package com.ruyuan.eshop.data.migrate.tool.service.impl;

import com.ruyuan.eshop.data.migrate.tool.model.order.*;
import com.ruyuan.eshop.data.migrate.tool.service.*;
import com.ruyuan.eshop.data.migrate.tool.util.LoggerFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhonghuashishan
 * @version 1.0
 */
@Service
public class HistoryDataSyncServiceImpl implements HistoryDataSyncService {

    private static Logger log = LoggerFactory.getLogger(HistoryDataSyncServiceImpl.class);

    @Autowired
    private OriginAfterSaleInfoService originAfterSaleInfoService;

    @Autowired
    private OriginAfterSaleItemService originAfterSaleItemService;

    @Autowired
    private OriginAfterSaleLogService originAfterSaleLogService;

    @Autowired
    private OriginAfterSaleRefundService originAfterSaleRefundService;

    @Autowired
    private OriginOrderAmountDetailService originOrderAmountDetailService;

    @Autowired
    private OriginOrderAmountService originOrderAmountService;

    @Autowired
    private OriginOrderAutoNoService originOrderAutoNoService;

    @Autowired
    private OriginOrderCancelScheduledTaskService originOrderCancelScheduledTaskService;

    @Autowired
    private OriginOrderDeliveryDetailService originOrderDeliveryDetailService;

    @Autowired
    private OriginOrderInfoService originOrderInfoService;

    @Autowired
    private OriginOrderItemService originOrderItemService;

    @Autowired
    private OriginOrderPaymentDetailService originOrderPaymentDetailService;

    @Autowired
    private OriginTendConsistencyTaskService originTendConsistencyTaskService;

    @Autowired
    private TargetAfterSaleInfoService targetAfterSaleInfoService;

    @Autowired
    private TargetAfterSaleItemService targetAfterSaleItemService;

    @Autowired
    private TargetAfterSaleLogService targetAfterSaleLogService;

    @Autowired
    private TargetAfterSaleRefundService targetAfterSaleRefundService;

    @Autowired
    private TargetOrderAmountDetailService targetOrderAmountDetailService;

    @Autowired
    private TargetOrderAmountService targetOrderAmountService;

    @Autowired
    private TargetOrderAutoNoService targetOrderAutoNoService;

    @Autowired
    private TargetOrderCancelScheduledTaskService targetOrderCancelScheduledTaskService;

    @Autowired
    private TargetOrderDeliveryDetailService targetOrderDeliveryDetailService;

    @Autowired
    private TargetOrderInfoService targetOrderInfoService;

    @Autowired
    private TargetOrderItemService targetOrderItemService;

    @Autowired
    private TargetOrderPaymentDetailService targetOrderPaymentDetailService;

    @Autowired
    private TargetTendConsistencyTaskService targetTendConsistencyTaskService;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public Long batchSync(String tableName, Long minId, Date lastGmtModified, int batchSize) {

        log.info(LoggerFormat.build()
                .remark("batchSync->request")
                .data("tableName", tableName)
                .data("minId", minId)
                .data("lastGmtModified", lastGmtModified)
                .data("batchSize", batchSize)
                .finish());


        if("after_sale_info".equalsIgnoreCase(tableName)) {
            List<AfterSaleInfoDO> afterSaleInfoDOList = originAfterSaleInfoService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(afterSaleInfoDOList != null && !afterSaleInfoDOList.isEmpty()) {
                maxId = afterSaleInfoDOList.get(afterSaleInfoDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetAfterSaleInfoService.batchInsert(afterSaleInfoDOList);

                return maxId + 1;
            }
            return null;
        }

        if("after_sale_item".equalsIgnoreCase(tableName)) {
            List<AfterSaleItemDO> afterSaleItemDOList = originAfterSaleItemService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(afterSaleItemDOList != null && !afterSaleItemDOList.isEmpty()) {
                maxId = afterSaleItemDOList.get(afterSaleItemDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetAfterSaleItemService.batchInsert(afterSaleItemDOList);

                return maxId + 1;
            }
            return null;
        }

        if("after_sale_log".equalsIgnoreCase(tableName)) {
            List<AfterSaleLogDO> afterSaleLogDOList = originAfterSaleLogService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(afterSaleLogDOList != null && !afterSaleLogDOList.isEmpty()) {
                maxId = afterSaleLogDOList.get(afterSaleLogDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetAfterSaleLogService.batchInsert(afterSaleLogDOList);

                return maxId + 1;
            }
            return null;
        }

        if("after_sale_refund".equalsIgnoreCase(tableName)) {
            List<AfterSaleRefundDO> afterSaleRefundDOList = originAfterSaleRefundService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(afterSaleRefundDOList != null && !afterSaleRefundDOList.isEmpty()) {
                maxId = afterSaleRefundDOList.get(afterSaleRefundDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetAfterSaleRefundService.batchInsert(afterSaleRefundDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_amount_detail".equalsIgnoreCase(tableName)) {
            List<OrderAmountDetailDO> orderAmountDetailDOList = originOrderAmountDetailService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderAmountDetailDOList != null && !orderAmountDetailDOList.isEmpty()) {
                maxId = orderAmountDetailDOList.get(orderAmountDetailDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderAmountDetailService.batchInsert(orderAmountDetailDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_amount".equalsIgnoreCase(tableName)) {
            List<OrderAmountDO> orderAmountDOList = originOrderAmountService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderAmountDOList != null && !orderAmountDOList.isEmpty()) {
                maxId = orderAmountDOList.get(orderAmountDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderAmountService.batchInsert(orderAmountDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_auto_no".equalsIgnoreCase(tableName)) {
            List<OrderAutoNoDO> orderAutoNoDOList = originOrderAutoNoService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderAutoNoDOList != null && !orderAutoNoDOList.isEmpty()) {
                maxId = orderAutoNoDOList.get(orderAutoNoDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderAutoNoService.batchInsert(orderAutoNoDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_cancel_scheduled_task".equalsIgnoreCase(tableName)) {
            List<OrderCancelScheduledTaskDO> orderCancelScheduledTaskDOList = originOrderCancelScheduledTaskService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderCancelScheduledTaskDOList != null && !orderCancelScheduledTaskDOList.isEmpty()) {
                maxId = orderCancelScheduledTaskDOList.get(orderCancelScheduledTaskDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderCancelScheduledTaskService.batchInsert(orderCancelScheduledTaskDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_delivery_detail".equalsIgnoreCase(tableName)) {
            List<OrderDeliveryDetailDO> orderDeliveryDetailDOList = originOrderDeliveryDetailService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderDeliveryDetailDOList != null && !orderDeliveryDetailDOList.isEmpty()) {
                maxId = orderDeliveryDetailDOList.get(orderDeliveryDetailDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderDeliveryDetailService.batchInsert(orderDeliveryDetailDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_info".equalsIgnoreCase(tableName)) {
            List<OrderInfoDO> orderInfoDOList = originOrderInfoService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderInfoDOList != null && !orderInfoDOList.isEmpty()) {
                maxId = orderInfoDOList.get(orderInfoDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderInfoService.batchInsert(orderInfoDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_item".equalsIgnoreCase(tableName)) {
            List<OrderItemDO> orderItemDOList = originOrderItemService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderItemDOList != null && !orderItemDOList.isEmpty()) {
                maxId = orderItemDOList.get(orderItemDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderItemService.batchInsert(orderItemDOList);

                return maxId + 1;
            }
            return null;
        }

        if("order_payment_detail".equalsIgnoreCase(tableName)) {
            List<OrderPaymentDetailDO> orderPaymentDetailDOList = originOrderPaymentDetailService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(orderPaymentDetailDOList != null && !orderPaymentDetailDOList.isEmpty()) {
                maxId = orderPaymentDetailDOList.get(orderPaymentDetailDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetOrderPaymentDetailService.batchInsert(orderPaymentDetailDOList);

                return maxId + 1;
            }
            return null;
        }

        if("ruyuan_tend_consistency_task".equalsIgnoreCase(tableName)) {
            List<TendConsistencyTaskDO> tendConsistencyTaskDOList = originTendConsistencyTaskService.list(minId, lastGmtModified, batchSize);
            Long maxId = null;
            if(tendConsistencyTaskDOList != null && !tendConsistencyTaskDOList.isEmpty()) {
                maxId = tendConsistencyTaskDOList.get(tendConsistencyTaskDOList.size() - 1).getId();

                // 这里可以考虑基于生产者-消费者模式，将数据放入阻塞内存队列，然后用一个线程池来异步消费 TODO
                targetTendConsistencyTaskService.batchInsert(tendConsistencyTaskDOList);

                return maxId + 1;
            }
            return null;
        }

        return null;
    }
}